//
//  CSRegisterViewController.m
//  ChinaShip
//
//  Created by Yale on 2019/2/22.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSRegisterViewController.h"

@interface CSRegisterViewController (){
    IBOutlet UITextField *loginTextField,*pwdTextField,*secondTextField;
    IBOutlet NSLayoutConstraint *hLayoutConstraint;
}

@end

@implementation CSRegisterViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self hiddenNavBar];
    if (isIPhonex) {
        hLayoutConstraint.constant = 40;
        [self.view layoutIfNeeded];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)backAction:(id)sender{
    [self popViewController];
}

- (IBAction)registerAction:(id)sender{
    if (loginTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"Account cannot be empty"];
        return;
    }
    
    if (pwdTextField.text.length == 0 || secondTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"Password can not be blank"];
        return;
    }
    
    if (pwdTextField.text.length < 6 || secondTextField.text.length < 6) {
        [SVProgressHUD showErrorWithStatus:@"Password length cannot be less than 6 digits"];
        return;
    }
    
    if (![pwdTextField.text isEqualToString:secondTextField.text]) {
        [SVProgressHUD showErrorWithStatus:@"Entering passwords twice is different"];
        return;
    }
    [SVProgressHUD show];
    [self performSelector:@selector(addRegister) withObject:nil afterDelay:0.8];
}

- (void)addRegister{
    BOOL isAdd = YES;
    NSMutableArray *dataArray = [CSTool getAllUsers];
    for (NSDictionary *dic in dataArray) {
        NSString *user = [dic objectForKey:@"user"];
        if ([user isEqualToString:loginTextField.text]) {
             [SVProgressHUD showErrorWithStatus:@"This account already exists"];
             isAdd = NO;
        }
    }
    if (isAdd) {
        [CSTool addUser:@{
                          @"user":loginTextField.text,
                          @"pwd" :pwdTextField.text
                          }];
        [SVProgressHUD showSuccessWithStatus:@"Registration is successful, please log in again"];
        [self popViewController];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
